export default {
  methods: {
    count(o) {
      let t = typeof o;
      if ('string' == t) {
        return o.length;
      } else if ('object' == t) {
        let n = 0;
        for (let i in o) {
          n++;
        }
        return n;
      }
      return 0;
    },

    // 进制转换
    getChangedDemical(Val, from, to) {
      if (from === 16) {
        Val = '0x' + Val;
        return parseInt(parseInt(Val), to);
      }

      if (from === 10) {
        return Val.toString(to).toUpperCase();
      }
    }
  }
};

export const remoteLoad = (url, hasCallback) => {
  return createScript(url);
  /**
   * 创建script
   * @param url
   * @returns {Promise}
   */
  function createScript(url) {
    var scriptElement = document.createElement('script');
    document.body.appendChild(scriptElement);
    scriptElement.addEventListener(
      'load',
      e => {
        removeScript(scriptElement);
      },
      false
    );

    scriptElement.addEventListener(
      'error',
      e => {
        removeScript(scriptElement);
      },
      false
    );

    if (hasCallback) {
      window.____callback____ = function() {
        resolve();
        window.____callback____ = null;
      };
    }

    if (hasCallback) {
      url += '&callback=____callback____';
    }

    scriptElement.src = url;
  }

  /**
   * 移除script标签
   * @param scriptElement script dom
   */
  function removeScript(scriptElement) {
    document.body.removeChild(scriptElement);
  }
};
